package com.trendytech.tds.common.bean;

import javax.servlet.http.HttpServletRequest;

/**
 * 默认的排序字段解析器，只根据CREATE_DATE排序.
 * 
 */
public class DefaultPagerOrderFieldParser implements IPagerOrderFieldParser {
	// 排序方式
	public static final String ORDER_ASC = "asc";
	public static final String ORDER_DESC = "desc";

	public static final SortProperty DEFAULT_ORDERBY_FIELD = new SortProperty("createDate", "create_date", ORDER_DESC);
	protected String orderByField;

	protected String orderType;

	public DefaultPagerOrderFieldParser() {
		super();
	}

	public DefaultPagerOrderFieldParser(HttpServletRequest request) {
		super();
		String sort = request.getParameter("sort");//硕正的排序参数 属性名空格a|d
		if(sort != null && !sort.equals("")){
			String[] tmp = sort.split(" ");
			setOrderByField(tmp[0]);
			setOrderType(tmp[1]);
		}
	}

	@Override
	public void setOrderByField(String orderByField) {
		this.orderByField = orderByField;
	}

	@Override
	public void setOrderType(String ascOrDesc) {
		if("a".equals(ascOrDesc)){
			this.orderType = ORDER_ASC;
		}else if("d".equals(ascOrDesc)){
			this.orderType = ORDER_DESC;
		}
	}

	public SortProperty parseOrderField() {
		return DefaultPagerOrderFieldParser.DEFAULT_ORDERBY_FIELD;
	}

	@Override
	public String parseOrderType() {
		if (ORDER_ASC.equalsIgnoreCase(orderType)) {
			return ORDER_ASC;
		} else if (ORDER_DESC.equalsIgnoreCase(orderType)) {
			return ORDER_DESC;
		}
		return ORDER_DESC;
	}
}
